<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class isp_komplain extends MY_Controller {
	
	function __construct()
	{
		parent::__construct(); 
		
		$this->load->model('isp_komplain_model', 'IspKomplain');
		$this->load->model('isp_komplain_penugasan_model', 'Penugasan');
		$this->secure_session();
	}
	
	function index(){
		$data['title'] = "Setup Komplain";			
		$data['Status']=$this->Penugasan->GetApp();
		viewByTemplate('isp_komplain/index', $data, 'grid');
	}

	function id_auto(){
		$query = $this->db->query("SELECT id_komplain FROM (SELECT * FROM isp_komplain ORDER BY id_komplain DESC) WHERE rownum <=1");
	 				if ($query->row_array()) {
	 					$getquery = $query->row_array();
	 					$rowcount = $getquery['id_komplain'];
	 					$autonum = $rowcount + 1;
	 				} else {
					$autonum = 1;
				}
		$data['id_komplain_auto'] = $autonum;

		$query = $this->db->query("SELECT kode_komplain FROM (SELECT * FROM isp_komplain ORDER BY kode_komplain DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_komplain'];
		else
			$MaxID = "KP0000000";

		$NextID = (int) substr($MaxID,2,7);

		$NextID++;
		
		$data['kode_komplain_auto'] = "KP" . sprintf("%07s",$NextID);

		$query = $this->db->query("SELECT kode_komplain FROM (SELECT * FROM isp_komplain ORDER BY kode_komplain DESC) WHERE rownum <= 1");
		$getquery = $query->row_array();

		if($getquery)
			$MaxID = $getquery['kode_komplain'];
		else
			$MaxID = "000";

		$NextID = (int) substr($MaxID,0,3);

		$NextID++;

		$bulan = date("m");
		$tahun = date("Y");
			
			$hasil = "";
			$iromawi =
			array( "01"=>"I","02"=>"II","03"=>"III",
					"04"=>"IV","05"=>"V","06"=>"VI",
					  "07"=>"VII","08"=>"VIII","09"=>"IX",
					  	"10"=>"X","11"=>"XI","12"=>"XII" );
			if(array_key_exists($bulan,$iromawi)){
				$hasil = $iromawi[$bulan];
			}

		$data['kode_komplain_auto'] = sprintf("%03s",$NextID) . "/ISP/TS/______/" . $hasil . "/" . $tahun;
		echo json_encode($data);
	}

	function edit_checkbox()
	{
		$data_kebutuhan = $this->input->post('jenis_kebutuhan');
		$data['kebutuhan']	= explode(',', $data_kebutuhan);

		//var_dump($data_kebutuhan);
		$data_permintaan = $this->input->post('jenis_permintaan');
		$data['permintaan'] = explode(',', $data_permintaan);	

		//$data['test'] = 123;

		echo json_encode($data);	
	}
	
	function getdata()
	{
		$this->secure_session();
		echo $this->IspKomplain->GetJSON();
	}

	function getdata2()
	{
		$this->secure_session();
		echo $this->IspKomplain->GetJSON2();
	}

	private function validation()
	{
		$error = new ErrorList();

		// Get Error Template
		$tpl_empty = $this->MessageTemplate("ER001");
		$tpl_duplicate = $this->MessageTemplate("ER002");


		// Validation		
		if (post("kode_komplain") == '') {
			$error->Add("kode_komplain", "Kode Komplain", $tpl_empty);
		} else if (post("mode") == "create" && intval($this->get_data("SELECT 1 FROM isp_komplain WHERE kode_komplain = '".@ereg_replace("[^a-zA-Z0-9_-]", "",@strtoupper(@trim(post("kode_komplain"))))."'")) > 0) {
			$error->Add("kode_komplain", "Kode Komplain", $tpl_duplicate);
		} else if (post("tgl_komplain") == '') {
			$error->Add("tgl_komplain", "Tanggal Komplain", $tpl_empty);
		} else if (post("kode_customer") == '') {
			$error->Add("kode_customer", "Kode Client", $tpl_empty);
		} else if (post("komplain") == '') {
			$error->Add("komplain", "Detail Komplain", $tpl_empty);
		} 

		return $error;
	}
	
	function ajax_post()
	{
		$is_error = 0;
		$ls_response = "";
		$ls_genid = "";

		$error = $this->validation();
		try
		{
			if (count( $error->Get() ) > 0) { $is_error = 1; }
			else
			{
				// Fill Post Data
				$data	=  array(
					'id_komplain' => $this->input->post('id_komplain'),
					'kode_komplain' => $this->input->post('kode_komplain'),
					'tgl_komplain' => $this->input->post('tgl_komplain'),
					'kode_customer' => $this->input->post('kode_customer'),
					'komplain' => $this->input->post('komplain'),
					'status' => $this->input->post('status'),
					'keterangan' => $this->input->post('keterangan'),
					'address' => $this->input->post('address'),
					'phone' => $this->input->post('phone'),
					/*'jenis_kebutuhan' => $this->input->post('jenis_kebutuhan'),*/
					/*'jenis_permintaan' => $this->input->post('jenis_permintaan'),*/
					'tanggal_selesai' => $this->input->post('tanggal_selesai'),
					);

				$data['tgl_komplain']	= ($data['tgl_komplain'] == '' ? NULL : date_format3($data['tgl_komplain'], 'YYYY-MM-DD'));
				$data['tanggal_selesai']	= ($data['tanggal_selesai'] == '' ? NULL : date_format3($data['tanggal_selesai'], 'YYYY-MM-DD'));
				
				if (post("mode") == "create")
				{
					//$data['create_date']	= NOW;
					//$data['create_user']	= USERID;

					$this->IspKomplain->Add($data);

					$ls_response = "Data Tersimpan";
					//App.audit_trail("CS001", "create/" + (data.role_id ?? ""));

				}
				else if (post("mode") == "edit")
				{
					//$data['modify_date']	= NOW;
					//$data['modify_user']	= USERID;
					
					$this->IspKomplain->Update($data, 
										array('id_komplain'	=> $data['id_komplain']));

					$ls_response = "Data Tersimpan['Edited']";
					//App.audit_trail("CS002", "edit/" + (data.role_id ?? ""));

				}
			}
		} catch(Exception $e) {
			$is_error = 1; $ls_response = $e->getMessage();
		}
		
		echo $this->PostResponse(json_encode(array(	'is_error'	=> $is_error,
													'fields'	=> $error->GetDictionary(),
													'msg'		=> $ls_response,
													'genid'		=> $ls_genid,
													'form_sender'=> post("form_sender"))
								));
	}

	function ajax_delete()
	{
		$ls_result	= "";
		$pesan		= "Data Terhapus";
		$id_komplain 	= get('id_komplain');
		
		if ($id_komplain != "")
		{
			// Delete Single
			$arr_rule	= array('id_komplain' => $id_komplain);
			$ls_result = $this->IspKomplain->DeleteSingle($arr_rule);

		}
		else
		{
			
			$num_row = intval($this->data['num_row']['deleted']);
			
			for ($i = 0; $i < $num_row; $i++)
			{
				$row	= $this->data['deleted'][$i];
				
				$arr_rule	= array('id_komplain' => $row['id_komplain']);
				$ls_result .= $this->IspKomplain->DeleteSingle($arr_rule);
			}
		}
		
		echo $pesan;
		echo "
			<script>
				loadPage(gs_path + '/' + controller + '/index');
			</script>
		";
	}
}
?>